﻿package com.gengine.resource.info
{
    import com.gengine.debug.*;
    import com.gengine.resource.*;
    import com.gengine.resource.encryption.*;
    import flash.display.*;
    import flash.utils.*;

    public class ImageInfo extends ResourceInfo
    {
        protected var _bitmapData:BitmapData;
        private var _isATF:Boolean;
        private var _ATFByteArray:ByteArray;
        public var bmpWidth:int = -1;
        public var bmpHeight:int = -1;
        private var _format:int = 0;

        public function ImageInfo(param1:Object = null)
        {
            super(param1);
            return;
        }// end function

        public function get isATF() : Boolean
        {
            return this._isATF;
        }// end function

        public function get format() : int
        {
            return this._format;
        }// end function

        override public function dispose() : void
        {
            if (!this.isLoaded)
            {
                return;
            }
            if (this._bitmapData)
            {
                try
                {
                    this._bitmapData.dispose();
                }
                catch (e:Error)
                {
                }
                this._bitmapData = null;
            }
            if (this._ATFByteArray)
            {
                this._ATFByteArray.clear();
                this._ATFByteArray = null;
            }
            this.bmpWidth = -1;
            this.bmpHeight = -1;
            isLoaded = false;
            isLoading = false;
            this._isATF = false;
            super.dispose();
            return;
        }// end function

        public function get ATFByteArray() : ByteArray
        {
            return this._ATFByteArray;
        }// end function

        override public function set data(param1:Object) : void
        {
            var _loc_2:EncryptionInfo = null;
            var _loc_3:uint = 0;
            var _loc_4:uint = 0;
            var _loc_5:uint = 0;
            super.data = param1;
            loaderInfo = param1 as LoaderInfo;
            if (loaderInfo)
            {
                this._bitmapData = Bitmap(loaderInfo.content).bitmapData as BitmapData;
            }
            else if (param1 is BitmapData)
            {
                this._bitmapData = BitmapData(param1);
            }
            if (param1 is EncryptionInfo)
            {
                _loc_2 = param1 as EncryptionInfo;
                this._isATF = _loc_2.contentType == EncryptUtils.ATF;
                switch(_loc_2.contentType)
                {
                    case EncryptUtils.TGA:
                    {
                        throw new Error("目前不支持TGA");
                    }
                    case EncryptUtils.DDS:
                    {
                        throw new Error("目前不支持DDS");
                    }
                    case EncryptUtils.ATF:
                    {
                        this._ATFByteArray = _loc_2.contentBytes;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            if (this._bitmapData)
            {
                this.bmpWidth = this._bitmapData.width;
                this.bmpHeight = this._bitmapData.height;
            }
            if (this._ATFByteArray)
            {
                if (this._ATFByteArray[6] == 255)
                {
                    this._ATFByteArray.position = 12;
                }
                else
                {
                    this._ATFByteArray.position = 6;
                }
                _loc_3 = this._ATFByteArray.readUnsignedByte();
                _loc_4 = _loc_3 >> 7;
                _loc_5 = _loc_3 & 127;
                if (_loc_5 <= 1)
                {
                    this._format = TextureFormat.FORMAT_COMPRESSED_BGRA;
                }
                else if (_loc_5 <= 3)
                {
                    this._format = TextureFormat.FORMAT_COMPRESSED;
                }
                else
                {
                    this._format = TextureFormat.FORMAT_COMPRESSED_ALPHA;
                }
                this.bmpWidth = Math.pow(2, this._ATFByteArray.readUnsignedByte());
                this.bmpHeight = Math.pow(2, this._ATFByteArray.readUnsignedByte());
            }
            else
            {
                this._format = TextureFormat.FORMAT_RGBA;
            }
            return;
        }// end function

        public function get bitmapData() : BitmapData
        {
            return this._bitmapData;
        }// end function

        override protected function unload(param1:LoaderInfo) : void
        {
            Log.system("unloadedImage:" + path);
            if (this._bitmapData)
            {
                this._bitmapData.dispose();
                this._bitmapData = null;
            }
            this._ATFByteArray = null;
            super.unload(param1);
            return;
        }// end function

    }
}
